#!/usr/bin/python

# Copyright (C) 2007, Ari Gordon-Schlosberg <regs@nebcorp.com>

# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
#     * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
# 
#     * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
# 
#     * Neither the name of the Ari Gordon-Schlosberg nor the names of
#     any other contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from re import compile
from os import listdir

ACPI_BATTERY_PATH='/proc/acpi/battery'
batt_name_re = compile("BAT(\d+)")
batt_full_cap_re = compile("^last full capacity:\s+(\d+) mWh$")
batt_level_re = compile("^remaining capacity:\s+(\d+) mWh$")
batt_rate_re = compile("^present rate:\s+(\d+) mW$");
batt_state_re = compile("^charging state:\s+(\w+)$")

def process_battery(batt_name):
    global  ACPI_BATTERY_PATH
    
    result = batt_name_re.search(batt_name)
    if result:
        batt_tag = result.group(1)
        
    infoh = open("%s/%s/info"%(ACPI_BATTERY_PATH,batt_name))
    #print "info"
    for line in infoh:
        #print line[0:-1]        
        result = batt_full_cap_re.search(line)
        if result:
            full_capacity = result.group(1)
            
    
    stateh = open("%s/%s/state"%(ACPI_BATTERY_PATH,batt_name))
    #print "state"
    for line in stateh:
        #print line[0:-1]
        result = batt_level_re.search(line)
        if result:
            current_level = result.group(1)
            
        result = batt_rate_re.search(line)
        if result:
            rate = result.group(1)

        result = batt_state_re.search(line)
        if result:
            state = result.group(1)
            
    percentage = float(current_level) / float(full_capacity)
    if state == "discharging":
            time_left = float(current_level) / float(rate)
            desc = "remaining"
    elif int(rate) > 0:
            time_left = (float(full_capacity) - float(current_level)) / float(rate)
            desc = "until charged"
    else:
            time_left = 0

    if time_left > 0:
	    hours_left = int(time_left)
	    minutes_left = (time_left - hours_left)  * 60
	    timestring = " (%d:%02d %s)"%(hours_left,minutes_left,desc) 
    else:
	    timestring =""
    print "Battery %s: %.2f%%%s"%(batt_tag,percentage * 100,timestring)
    

#dirh = opendir(ACPI_BATTERY_PATH)

for batt_name in listdir(ACPI_BATTERY_PATH):
    process_battery(batt_name)

